<?php
#### CONFIG MANAGEMENT CONTROLLER ##############################################
class ConfigController {

	#### CONSTRUCTOR ###########################################################
	function ConfigController() {
		Base::getThis();

		// Set the page title
		$this->page_title = _('Config');

		// Perform default function
		invoke_method($this, url(3));
	}

	#### MAIN #################################################################
	function main() {

		// If we come here after a redirect, display this notice
		if (url('search', 'config-updated')) $feedback = notice(_('The positions have been saved.'));
		else $feedback = NULL;

		// Set output
		$title = $this->icon['config'].' '._('Manage Configuration');
		$body = $this->buildForm();
		$this->output .= box($title, $feedback.$body);
	}

	#### BUILD FORM ############################################################
	function buildForm() {

		$form = $this->load->library('form');

		$form->open(url(1, 2).'/config-update');
		$form->openFieldset(_('Site'));
		$form->text(_('Name').'*', 'name', $this->config['name']);
		$form->text(_('Slogan'), 'slogan', $this->config['slogan']);
		$form->text(_('URL').'*', 'url', $this->config['url']);
		$form->text(_('Email').'*', 'email', $this->config['email']);
		$form->select(_('Locale'), 'locale', $this->getLocales(), $this->config['locale']);
		$form->select(_('Theme'), 'theme', $this->getThemes(), $this->config['theme']);
		$form->openGroup(_('Custom Footer'), TRUE);
		$form->textEditor(NULL, 'custom_footer', $this->config['custom_footer']);
		$form->closeGroup();
		$form->closeFieldset();
		$form->openFieldset(_('Meta'));
		$form->textArea(_('Keywords'), 'keywords', $this->config['keywords']);
		$form->textArea(_('Description'), 'description', $this->config['description']);
		$form->closeFieldset();
		$form->openFieldset(_('Close Site'));
		$form->checkbox(_('Close Site?'), 'site_closed', $this->config['site_closed']);
		$form->textArea(_('Closed Message'), 'site_closed_message', $this->config['site_closed_message']);
		$form->closeFieldset();
		$form->openFieldset(_('Miscellaneous'));
		$form->checkbox(_('BBcode'), 'bbcode_enabled', $this->config['bbcode_enabled']);
		$form->checkbox(_('Smilies'), 'smilies_enabled', $this->config['smilies_enabled']);
		$form->checkbox(_('SQL Cache'), 'sql_cache_enabled', $this->config['sql_cache_enabled']);
		$form->checkbox(_('HTML Cache'), 'html_cache_enabled', $this->config['html_cache_enabled']);
		$form->checkbox(_('Debug'), 'debug_enabled', $this->config['debug_enabled']);
		$form->checkbox(_('Report SQL Errors'), 'report_sql_errors', $this->config['report_sql_errors']);
//		$form->checkbox(_('Text Editor'), 'text_editor', $this->config['text_editor']);
		$form->closeFieldset();
		$form->button('submit', _('Update'));
		$form->close();

		return $form->output();
	}

	#### GET THEMES ############################################################
	function getThemes() {
		if ($handle = opendir('templates/themes')) {
		    while (false !== ($file = readdir($handle))) {
		        if (!strstr($file, '.')) {
		        	$file = str_replace('_', ' ', $file);
					$templates[$file] = ucwords($file);
		        }
		    }
		    closedir($handle);
		}

		return $templates;
	}

	#### GET LOCALES ###########################################################
	function getLocales() {

		// Defaults
		$locales['en_GB'] = 'en_GB';
		$locales['en_US'] = 'en_US';

		if ($handle = opendir('locale')) {
		    while (false !== ($file = readdir($handle))) {
		        if (!strstr($file, '.')) {
					$locales[$file] = $file;
		        }
		    }
		    closedir($handle);
		}

		return $locales;
	}

	#### CONFIG UPDATE #########################################################
	function configUpdate() {

		$this->db->update($this->db->table['config'], $this->post);
		$this->db->query() or sql_error(__FILE__, __LINE__);

		$this->load->helper('cache');
		delete_cache('config', TRUE);

		echo '<script type="text/javascript">
			window.location = "'.$this->config['url'].url(1, 2).'/config-updated";
		</script>';

		// We don't need to return anything to $this->output() in index.php so die()
		die();
	}
}
?>